
using System;
using System.Collections;

namespace GeoFramework.IO
{
	/// <summary>
	/// Represents a collection of Packet objects.
	/// </summary>
	public class PacketCollection : IList, ICollection, IEnumerable
	{
		private ArrayList pItems;		

		/// <summary>Represents a collection containing no items.</summary>
		public static readonly PacketCollection Empty = new PacketCollection(0);

		/// <summary>Creates a new instance.</summary>
		public PacketCollection()
		{
			pItems = new ArrayList();
		}

		public PacketCollection(int capacity)
		{
			pItems = new ArrayList(capacity);
		}

		internal PacketCollection(PacketCollection collection)
		{
			pItems = ArrayList.Synchronized(new ArrayList(collection));
		}

		/// <summary>
		/// Creates a thread-safe copy of the current collection.
		/// </summary>
		/// <param name="collection"></param>
		/// <returns></returns>
		public static PacketCollection Synchronized(PacketCollection collection)
		{
			return new PacketCollection(collection);
		}

		/// <summary>Adds a packet to the collection.</summary>
		public int Add(Packet packet)
		{
            //lock (pItems.SyncRoot)
			{
				return pItems.Add(packet);
			}
		}

		/// <summary>Indicates if the collection can be modified.</summary>
		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		/// <summary>Returns or sets a packet at the specified index.</summary>
		public Packet this[int index]
		{
			get
			{
				return (Packet)pItems[index];
			}
			set
			{
				lock(pItems.SyncRoot)
				{
					pItems[index] = value;
				}
			}
		}

		public object SyncRoot
		{
			get
			{
				return pItems.SyncRoot;
			}
		}

		/// <summary>Removes the packet at the specified index.</summary>
		public void RemoveAt(int index)
		{
//            lock (pItems.SyncRoot)
			{
				pItems.RemoveAt(index);
			}
		}

		/// <summary>Adds a packet to the collection at the specified index.</summary>
		public void Insert(int index, Packet value)
		{
//            lock (pItems.SyncRoot)
			{
				pItems.Insert(index, value);
			}
		}

		/// <summary>Removes the specified packet from the collection.</summary>
		public void Remove(Packet value)
		{
//            lock (pItems.SyncRoot)
			{
				pItems.Remove(value);
			}
		}

		/// <summary>Indicates if the collection contains the specified packet.</summary>
		public bool Contains(Packet value)
		{
			return pItems.Contains(value);
		}

		/// <summary>Removes all packets from the collection.</summary>
		public void Clear()
		{
//            lock (pItems.SyncRoot)
			{
				pItems.Clear();
			}
		}

		/// <summary>Returns the location of the specified packet in the collection.</summary>
		public int IndexOf(Packet value)
		{
			return pItems.IndexOf(value);
		}

		/// <summary>Indicates if the collection has a set limit of packets.</summary>
		public bool IsFixedSize
		{
			get
			{
				return false;
			}
		}

		/// <summary>Returns the number of packets in the collection.</summary>
		public int Count
		{
			get
			{
				return pItems.Count;
			}
		}

		/// <summary>Copies the current collection to the specified array.</summary>
		public void CopyTo(Array array, int index)
		{
//            lock (pItems.SyncRoot)
			{
				pItems.CopyTo(array, index);
			}
		}

		#region IList Members

		bool IList.IsReadOnly
		{
			get
			{
				return this.IsReadOnly;
			}
		}

		object IList.this[int index]
		{
			get
			{
				return this[index];
			}
			set
			{
				this[index] = (Packet)value;
			}
		}

		void IList.RemoveAt(int index)
		{
			this.RemoveAt(index);
		}

		void IList.Insert(int index, object value)
		{
			this.Insert(index, (Packet)value);
		}

		void IList.Remove(object value)
		{
			this.Remove((Packet)value);
		}

		bool IList.Contains(object value)
		{
			return this.Contains((Packet)value);
		}

		void IList.Clear()
		{
			this.Clear();
		}

		int IList.IndexOf(object value)
		{
			return this.IndexOf((Packet)value);
		}

		int IList.Add(object value)
		{
			return this.Add((Packet)value);
		}

		bool IList.IsFixedSize
		{
			get
			{
				return this.IsFixedSize;
			}
		}

		#endregion

		#region ICollection Members

		bool ICollection.IsSynchronized
		{
			get
			{
				return true;
			}
		}

		int ICollection.Count
		{
			get
			{
				return this.Count;
			}
		}

		void ICollection.CopyTo(Array array, int index)
		{
			this.CopyTo(array, index);
		}

		object ICollection.SyncRoot
		{
			get
			{
				return this.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>Returns an enumerator used for iterating through the collection.</summary>
		public IEnumerator GetEnumerator()
		{
			return pItems.GetEnumerator();
		}

		#endregion
	}


	public class PacketCollectionEventArgs : EventArgs
	{
		private PacketCollection oPacketCollection;

		public static readonly new PacketCollectionEventArgs Empty = new PacketCollectionEventArgs(PacketCollection.Empty);

		public PacketCollectionEventArgs(PacketCollection packetCollection)
		{
			oPacketCollection = packetCollection;
		}

		public PacketCollection PacketCollection
		{
			get
			{
				return oPacketCollection;
			}
		}
	}

	//public delegate void PacketCollectionEventHandler(object sender, EventArgs e);

}
